﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using XDerive.XStudio.Common.CommonAppI;
using XDerive.XStudio.SystemFramework;
using WebStudio.Mvc.Core.Helper;

namespace WebStudio.Mvc.Core.Extensions
{
    /// <summary>
    /// 复合参数扩展函数（XHtmlhelper）
    /// </summary>
    public static class ComplexParameterExtension
    {
        #region 下拉框控件
        /// <summary>
        /// 复合参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="complexParameterTypeCodeID">复合参数类型代码</param>
        /// <returns></returns>
        public static MvcHtmlString ComplexParameterDropDownList(this XHtmlHelper xHelper, string name, int complexParameterTypeCodeID)
        {
            return ComplexParameterDropDownList(xHelper, name, complexParameterTypeCodeID, false);
        }
        /// <summary>
        /// 复合参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="complexParameterTypeCodeID">复合参数类型代码</param>
        /// <param name="isShowComplexParameterTypeName">是否显示复合参数类型名称</param>
        /// <returns></returns>
        public static MvcHtmlString ComplexParameterDropDownList(this XHtmlHelper xHelper, string name, int complexParameterTypeCodeID, bool isShowComplexParameterTypeName)
        {
            return ComplexParameterDropDownList(xHelper, name, complexParameterTypeCodeID, isShowComplexParameterTypeName, null);
        }
        /// <summary>
        /// 复合参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="complexParameterTypeCodeID">复合参数类型代码</param>
        /// <param name="isShowComplexParameterTypeName">是否显示复合参数类型名称</param>
        /// <param name="selectedValue">选中值</param>
        /// <returns></returns>
        public static MvcHtmlString ComplexParameterDropDownList(this XHtmlHelper xHelper, string name, int complexParameterTypeCodeID, bool isShowComplexParameterTypeName, string selectedValue)
        {
            return ComplexParameterDropDownList(xHelper, name, complexParameterTypeCodeID, isShowComplexParameterTypeName, selectedValue, null);
        }

        /// <summary>
        /// 复合参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="complexParameterTypeCodeID">复合参数类型代码</param>
        /// <param name="isShowComplexParameterTypeName">是否显示复合参数类型名称</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="searchCondition"></param>
        /// <returns></returns>
        public static MvcHtmlString ComplexParameterDropDownList(this XHtmlHelper xHelper, string name, int complexParameterTypeCodeID, bool isShowComplexParameterTypeName, string selectedValue, string searchCondition)
        {
            return ComplexParameterDropDownList(xHelper, name, complexParameterTypeCodeID, isShowComplexParameterTypeName, selectedValue, searchCondition, null);
        }

        /// <summary>
        /// 复合参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="complexParameterTypeCodeID">复合参数类型代码</param>
        /// <param name="isShowComplexParameterTypeName">是否显示复合参数类型名称</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ComplexParameterDropDownList(this XHtmlHelper xHelper, string name, int complexParameterTypeCodeID, bool isShowComplexParameterTypeName, string selectedValue, object htmlAttributes)
        {
            return ComplexParameterDropDownList(xHelper, name, complexParameterTypeCodeID, isShowComplexParameterTypeName, selectedValue, null, htmlAttributes);
        }

        /// <summary>
        /// 复合参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="complexParameterTypeCodeID">复合参数类型代码</param>
        /// <param name="isShowComplexParameterTypeName">是否显示复合参数类型名称</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="searchCondition">过滤条件，如：ParameterCodeID > 9</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ComplexParameterDropDownList(this XHtmlHelper xHelper, string name, int complexParameterTypeCodeID, bool isShowComplexParameterTypeName, string selectedValue, string searchCondition, object htmlAttributes)
        {
            List<SelectListItem> param = new List<SelectListItem>();
            if (isShowComplexParameterTypeName)
            {
                param.Add(new SelectListItem() { Text = SysFunction.StringParse(DictionaryAppI.GetScalar("Sys_ComplexParameterTypeTB", "ComplexParameterTypeName", "ComplexParameterTypeCodeID = " + complexParameterTypeCodeID)), Value = "0", Selected = (selectedValue != null && selectedValue == "0") });
            }

            //条件
            string resultCondition = string.Format("ComplexParameterTypeCodeID = {0}", complexParameterTypeCodeID);
            if (!string.IsNullOrEmpty(searchCondition))
            {
                resultCondition = string.Format("{0} AND ({1})", resultCondition, searchCondition);
            }
            int complexParameterLevel = 0;
            using (IDataReader dr = new ComplexParameterAppI().GetComplexParameterDataReader(resultCondition))
            {
                while (dr.Read())
                {
                    complexParameterLevel = isShowComplexParameterTypeName ? SysFunction.IntParse(dr["ComplexParameterLevel"]) : SysFunction.IntParse(dr["ComplexParameterLevel"]) - 1;
                    string item = "";
                    for (int i = 0; i < complexParameterLevel; i++)
                    {
                        item = item + "　";
                    }
                    string itemName = isShowComplexParameterTypeName ? string.Format("{0}{1}", item, dr["ComplexParameterName"]) : dr["ComplexParameterName"].ToString();
                    param.Add(new SelectListItem() { Text = itemName, Value = SysFunction.StringParse(dr["ComplexParameterCode"]), Selected = (selectedValue != null && selectedValue == SysFunction.StringParse(dr["ComplexParameterCode"])) });
                }
            }
            return xHelper.Html.DropDownList(name, param, htmlAttributes);
        }
        #endregion
    }
}
